<?php

namespace App\Store\Models\Basic;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;

class GoodsCategory extends Model
{
    protected $table = 'site_basic_goods_category';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo','cabinetgroupinfo','metalinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        $notUseStoreIds =SiteStore::query()->where('is_can_use',2)
            ->where('is_del',1)
            ->get(['id'])
            ->pluck('id')
            ->toArray();

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        if(!empty($notUseStoreIds)){
            $query->whereNotIn('store_id', $notUseStoreIds);
        }

        $store_id = $request->store_id;//下拉单选搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }
        $is_recovery = $request->is_recovery??0;
        if ($is_recovery>0) {
            $query->where('is_recovery', $is_recovery);
        }

        $querystore_ids= $request->store_ids;//下拉多选搜索
        if($querystore_ids){
            $querystore_ids=is_array($querystore_ids)?$querystore_ids:(is_array(json_decode($querystore_ids,true))?json_decode($querystore_ids,true):[]);
            $query->whereIn('store_id', $querystore_ids);
        }



        $isgetall = intval($request->isgetall) ?? 0;

        $category_name = $request->category_name;
        if ($category_name != '') {
            $query->where('category_name', $category_name);
        }

        $goods_cabinetgroup_id = intval($request->goods_cabinetgroup_id);
        if ($goods_cabinetgroup_id>0) {
            $query->where('goods_cabinetgroup_id', $goods_cabinetgroup_id);
        }

        $goods_metal_id = intval($request->goods_metal_id);
        if ($goods_metal_id>0) {
            $query->where('goods_metal_id', $goods_metal_id);
        }

        $is_recovery = $request->is_recovery;
        if (in_array($is_recovery, [1, 2])) {
            $query->where('is_recovery', $is_recovery);
        }

        $is_sujin = $request->is_sujin;
        if (in_array($is_sujin, [1, 2])) {
            $query->where('is_sujin', $is_sujin);
        }

        $is_virtually = $request->is_virtually;
        if (in_array($is_virtually, [1, 2])) {
            $query->where('is_virtually', $is_virtually);
        }
        $is_not_virtually = $request->is_not_virtually??0;
        if ($is_not_virtually>0) {
            $query->where('is_virtually','<>' ,$is_not_virtually);
        }


        $is_maintenance_price = $request->is_maintenance_price;
        if (in_array($is_maintenance_price, [1, 2])) {
            $query->where('is_maintenance_price', $is_maintenance_price);
        }

        $is_gram = $request->is_gram??0;
        if (in_array($is_gram, [1, 2])) {
            $query->where('is_gram', $is_gram);
        }

        $ids = $request->ids ?? [];

        // 如果是字符串，尝试解析JSON
        if (is_string($ids)) {
            $decoded = json_decode($ids, true);
            $ids = is_array($decoded) ? $decoded : [];
        }

        // 确保最终是数组类型
        if (!is_array($ids)) {
            $ids = [];
        }

        if (count($ids) > 0) {
            $query->whereIn('id', $ids);
        }



        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }


    public function cabinetgroupinfo(){
        return $this->hasOne(GoodsCabinetgroup::class, 'id', 'goods_cabinetgroup_id');
    }

    public function metalinfo(){
        return $this->hasOne(GoodsMetal::class, 'id', 'goods_metal_id');
    }



    public static function getListByStore($request){
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::where(['cp_id' => $cp_id, 'is_del' => 1]);
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $querystore_ids= $request->store_ids;//下拉单选搜索

        $querystore_ids = is_array($querystore_ids) ? $querystore_ids : (is_array(json_decode($querystore_ids, true)) ? json_decode($querystore_ids, true) : []);
        if (count($querystore_ids)<=0) {
            return ['list' => []];
        }
        $query->whereIn('store_id', $querystore_ids);

        $query->groupBy('category_name')->havingRaw('count(*)='.count($querystore_ids));
        $list = $query->select(['category_name'])->get()->toArray();
        return ['list' => $list];
    }

}
